package common;

import java.io.ByteArrayInputStream;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;

import easytalk.media.EasyTalkImages;

public class Locutor {

	private BodySet parentSet;
	private String id;
	private String name;
	private Image image = EasyTalkImages.DEFAULT_AVATAR;
	private Image bigImage = EasyTalkImages.DEFAULT_AVATAR;
	private boolean present = false;
	private boolean isTalking = false;
	private LocatorStatus status = new LocatorStatus();
	private boolean pictureLoaded = false;

	public Locutor(BodySet parent, String id) {
		this.parentSet = parent;
		this.id = id;
	}
	
	public String getShowName() {
		if (name==null)
			return id;
		return name;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Locutor) {
			return ((Locutor) obj).getId().equals(this.id);
		}
		return super.equals(obj);
	}

	public Image getBigImage() {
		return bigImage;
	}

	public String getId() {
		return id;
	}

	public Image getImage() {
		return image;
	}

	public String getName() {
		return name;
	}

	public BodySet getParentSet() {
		return parentSet;
	}

	public LocatorStatus getStatus() {
		return status;
	}

	public boolean isPresent() {
		return present;
	}

	public boolean isTalking() {
		return isTalking;
	}

	public void setBigImage(Image bigImage) {
		this.bigImage = bigImage;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setImage(byte[] avatar) {
		if (avatar==null)
			return;
		ByteArrayInputStream inputStream = new ByteArrayInputStream(avatar);
		ImageData bigImageData = new ImageData(inputStream);
		ImageData iData = bigImageData.scaledTo(40, 40);
		Image image = new Image(Display.getCurrent(), iData);
		this.image = image;
		this.bigImage = new Image(Display.getCurrent(), bigImageData);
		pictureLoaded = true;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setParentSet(BodySet parentSet) {
		this.parentSet = parentSet;
	}

	public void setPresent(boolean present) {
		this.present = present;
	}

	public void setStatus(LocatorStatus status) {
		this.status = status;
	}

	public void setTalking(boolean isTalking) {
		this.isTalking = isTalking;
	}

	public boolean isPictureLoaded() {
		
		return pictureLoaded ;
	}
	
	/**
	 * directly send an IM to someone
	 * @param toBeSent Message to be sent
	 */
	public void sendFastMessage(String toBeSent) {
		getParentSet().getConnection().sendFastMessage(id,toBeSent);
	}

}
